commonlibsse_ng\re\i/
IMemoryStoreBase.rs

1use crate::re::offsets_rtti::RTTI_IMemoryStoreBase;
2use crate::re::offsets_vtable::VTABLE_IMemoryStoreBase;
3use crate::rel::id::VariantID;
4use core::ffi::c_char;
5
6/// Represents memory statistics.
7#[repr(C)]
8#[derive(Debug)]
9pub struct MemoryStats {
10    pub name: *const c_char,  // 0x00
11    pub usedSize: usize,      // 0x08
12    pub committedSize: usize, // 0x10
13    pub reservedSize: usize,  // 0x18
14    pub overhead: u32,        // 0x20
15    pub pad24: u32,           // 0x24
16    pub freeSize: usize,      // 0x28
17}
18
19const _: () = {
20    assert!(std::mem::size_of::<MemoryStats>() == 0x30);
21};
22
23impl Default for MemoryStats {
24    #[inline]
25    fn default() -> Self {
26        unsafe { core::mem::zeroed() }
27    }
28}
29
30/// Virtual table for `IMemoryStoreBase`.
31#[repr(C)]
32pub struct IMemoryStoreBaseVtbl {
33    /// C++ `virtual ~IMemoryStoreBase`
34    pub CxxDrop: unsafe extern "C" fn(this: *mut IMemoryStoreBase),
35    pub Size: unsafe extern "C" fn(this: *const IMemoryStoreBase, mem: *const u8) -> usize,
36    pub GetMemoryStats: unsafe extern "C" fn(this: *mut IMemoryStoreBase, stats: *mut MemoryStats),
37    pub ContainsBlockImpl:
38        unsafe extern "C" fn(this: *const IMemoryStoreBase, block: *const u8) -> bool,
39}
40
41impl Default for IMemoryStoreBaseVtbl {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl IMemoryStoreBaseVtbl {
48    pub const fn new() -> Self {
49        const unsafe extern "C" fn CxxDrop(this: *mut IMemoryStoreBase) {
50            let _ = this;
51        }
52        pub const unsafe extern "C" fn Size(
53            this: *const IMemoryStoreBase,
54            mem: *const u8,
55        ) -> usize {
56            let _ = this;
57            let _ = mem;
58            0
59        }
60        pub const unsafe extern "C" fn GetMemoryStats(
61            this: *mut IMemoryStoreBase,
62            stats: *mut MemoryStats,
63        ) {
64            let _ = this;
65            let _ = stats;
66        }
67        pub const unsafe extern "C" fn ContainsBlockImpl(
68            this: *const IMemoryStoreBase,
69            block: *const u8,
70        ) -> bool {
71            let _ = this;
72            let _ = block;
73            false
74        }
75
76        Self { CxxDrop, Size, GetMemoryStats, ContainsBlockImpl }
77    }
78}
79static I_MEMORY_STORE_BASE_VTBL: IMemoryStoreBaseVtbl = IMemoryStoreBaseVtbl::new();
80
81/// Base memory store interface with virtual functions.
82#[repr(C)]
83#[derive(Debug, Clone, PartialEq)]
84pub struct IMemoryStoreBase {
85    pub vtable: *const IMemoryStoreBaseVtbl,
86}
87
88impl Default for IMemoryStoreBase {
89    fn default() -> Self {
90        Self::new()
91    }
92}
93
94impl IMemoryStoreBase {
95    /// Address & Offset of the runtime type information (RTTI) identifier.
96    pub const RTTI: VariantID = RTTI_IMemoryStoreBase;
97
98    /// Address & Offset of the virtual function table.
99    pub const VTABLE: [VariantID; 1] = VTABLE_IMemoryStoreBase;
100
101    pub const fn new() -> Self {
102        Self { vtable: &I_MEMORY_STORE_BASE_VTBL }
103    }
104
105    /// Destructor
106    ///
107    /// # Safety
108    /// Avoid double free
109    pub unsafe fn drop(&mut self) {
110        unsafe {
111            ((*self.vtable).CxxDrop)(self);
112        }
113    }
114
115    /// Get size of a memory block.
116    #[allow(clippy::not_unsafe_ptr_arg_deref)]
117    pub fn size(&self, mem: *const u8) -> usize {
118        unsafe { ((*self.vtable).Size)(self, mem) }
119    }
120
121    /// Get memory statistics.
122    pub fn get_memory_stats(&mut self, stats: &mut MemoryStats) {
123        unsafe {
124            ((*self.vtable).GetMemoryStats)(self, stats);
125        }
126    }
127
128    /// Check if the block is contained in the memory store.
129    #[allow(clippy::not_unsafe_ptr_arg_deref)]
130    pub fn contains_block_impl(&self, block: *const u8) -> bool {
131        unsafe { ((*self.vtable).ContainsBlockImpl)(self, block) }
132    }
133}
134
135const _: () = {
136    assert!(std::mem::size_of::<IMemoryStoreBase>() == 0x8);
137};